package hibernateDAOS;

import hibernate.HibernateUtil;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;


import beans.Carta;

public class CartaDAO {
	
	private static CartaDAO instancia = null;
	private static SessionFactory sf = null;

	public static CartaDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new CartaDAO();
		} 
		return instancia;
	}
	
	public void guardarCarta(Carta c){
		Session session = sf.openSession();
		session.beginTransaction();
		session.save(c);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public void eliminarCarta(Carta c){
		Session session = sf.openSession();
		session.beginTransaction();
		session.delete(c);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public void modificarCarta(Carta c){
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(c);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	@SuppressWarnings("unchecked")
	public List<Carta> obtenerCartas(){
		Session session = sf.openSession();
		Query q = session.createQuery("from Cartas");
		List<Carta> cartas = q.list();
		session.close();
		return cartas;
	}
}


